import Vue from 'vue'
import Vuex from 'vuex'
import http from '../axios'
// 导入配置好的路由实例
import router from '../router/index'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 用户的菜单
    menulist: [],
    loginData:{}
  },
  // 计算属性
  getters:{
    myList(state){
      return state.menulist
    },
    menuData(state){
      return state.loginData.menus
    }
  },
  // 同步修改源数据的唯一方法
  mutations: {
    getMenu(state, list){
      state.menulist = list;
    },

    login(state,data){
      state.loginData = data
    }
  },
  // 异步修改
  actions: {
    async getMenuInfo({commit}){
      console.log('actions');
      let res = await http('/menulist');
      commit('getMenu',res.list)
    },
    async ac_login({commit},obj){
        // console.log(obj);
        // let res = await http.post('/userlogin',obj)
        // console.log(res);
       let res = await http.post('/userlogin',obj)
        if(res.code == 200){
          // 判断登录成功后 把返回的数据发送到vuex仓库
          commit('login',res.list)
          alert(res.msg);
          router.push({
            path:'/'
          })
        }else{
          alert(res.msg);
         
        }
    },

  },
  modules: {

  }
})
